<?php
$appid = $_POST['appId'];
$appsecret = $_POST['appSecret'];
$locUrl = $_POST['locUrl'];
//请求
function wx_curl($url, $type = 'get', $res = 'json', $arr = '') {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if ($type == 'post') {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
    }
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

//生成随机的16位nonceStr
function getNonceStr(){
    $array = array_merge(range('a', 'z'),range('A', 'Z'),range(0, 9));
    $str = implode($array);
    $tmpstr = substr(str_shuffle($str),0,16);
    return $tmpstr;
}

//获取token
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
$arr = json_decode(wx_curl($url), true);
$access_token = $arr['access_token'];

//获取ticket和expires_in
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" . $access_token . "&type=jsapi";
$arr = json_decode(wx_curl($url), true);
$jsapi_ticket = $arr['ticket'];
$expires_in = $arr['expires_in'];

$timestamp = time();
$nonceStr = getNonceStr();

$signature = sha1("jsapi_ticket=".$jsapi_ticket."&noncestr=".$nonceStr."&timestamp=".$timestamp."&url=".$locUrl);


$arr = array('timestamp'=>$timestamp,'nonceStr'=>$nonceStr,'signature'=>$signature,'expires_in'=>$expires_in,'locUrl'=>$locUrl);
$json = json_encode($arr);

echo $json;